﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using Jumbot.Common;
using JumbotCms.Common;
namespace JumbotCms.Modules.Version.UserSource
{
    public partial class _ajax : JumbotCms.API.UserCenter
    {
        private string _operType = string.Empty;
        private string _response = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            id = Str2Str(q("id"));
            User_Load("", "json");
            this._operType = q("oper");
            switch (this._operType)
            {
                case "checkname":
                    ajaxCheckName();
                    break;
                case "ajaxGetList":
                    ajaxGetList();
                    break;
                default:
                    DefaultResponse();
                    break;
            }
            Response.Write(this._response);
        }

        private void DefaultResponse()
        {
            this._response = JsonResult(0, "未知操作");
        }
        private void ajaxCheckName()
        {
            if (id == "0")
            {
                doh.Reset();
                doh.ConditionExpress = "title=@title";
                doh.AddConditionParameter("@title", q("txtTitle"));
                if (doh.Exist("jcms_module_version_source"))
                    this._response = JsonResult(0, "不可添加");
                else
                    this._response = JsonResult(1, "可以添加");
            }
            else
            {
                doh.Reset();
                doh.ConditionExpress = "title=@title and id<>" + id;
                doh.AddConditionParameter("@title", q("txtTitle"));
                if (doh.Exist("jcms_module_version_source"))
                    this._response = JsonResult(0, "不可修改");
                else
                    this._response = JsonResult(1, "可以修改");
            }
        }
        private void ajaxGetList()
        {
            User_Load("", "json");
            int page = Int_ThisPage();
            int PSize = Str2Int(q("pagesize"), 20);
            int countNum = 0;
            string sqlStr = "";
            string joinStr = "A.[UserId]=B.Id";
            string whereStr1 = "A.[UserId]=" + UserId;//外围条件(带A.)
            string whereStr2 = "UserId=" + UserId;//分页条件(不带A.)
            string state = q("state");
            switch (state)
            {
                case "0":
                    whereStr1 += " AND A.State=0";
                    whereStr2 += " AND State=0";
                    break;
                case "1":
                    whereStr1 += " AND A.State=1";
                    whereStr2 += " AND State=1";
                    break;
                case "2":
                    whereStr1 += " AND A.State=2";
                    whereStr2 += " AND State=2";
                    break;
            }
            doh.Reset();
            doh.ConditionExpress = whereStr2;
            countNum = doh.Count("jcms_module_version_source");
            sqlStr = Jumbot.Common.staticFunction.GetSql("A.id as id,A.Title2 as Title2,B.UserName as UserName,B.TrueName as TrueName,A.State as State,A.IsLocked as IsLocked,A.AddDate as AddDate,A.Author as Author", "jcms_module_version_source", "jcms_normal_user", "Id", PSize, page, "desc", joinStr, whereStr1, whereStr2);
            doh.Reset();
            doh.SqlCmd = sqlStr;
            DataTable dt = doh.GetDataTable();
            this._response = "{result :\"1\"," +
                "returnval :\"操作成功\"," +
                "pagerbar :\"" + Jumbot.Common.HtmlPager.GetPageBar(3, "js", 2, countNum, PSize, page, "javascript:ajaxList(<#page#>);") + "\"," +
                Jumbot.Common.JsonHelp.DtToSON(dt, (PSize * (page - 1))) +
                "}";
            dt.Clear();
            dt.Dispose();
        }
    }
}